home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / DirectMusic / Tutorials / Tut2 / audtut2.frm (.txt) next >
Encoding:
Visual Basic Form  |  2001-10-08  |  10.5 KB  |  293 lines

  1. VERSION 5.00
  2. Begin VB.Form AudTut2 
  3.    BorderStyle     =   3  'Fixed Dialog
  4.    Caption         =   "Audio Tutorial 2"
  5.    ClientHeight    =   3405
  6.    ClientLeft      =   45
  7.    ClientTop       =   330
  8.    ClientWidth     =   3390
  9.    Icon            =   "audtut2.frx":0000
  10.    LinkTopic       =   "Form1"
  11.    MaxButton       =   0   'False
  12.    MinButton       =   0   'False
  13.    ScaleHeight     =   3405
  14.    ScaleWidth      =   3390
  15.    StartUpPosition =   3  'Windows Default
  16.    Begin VB.CommandButton cmdStop 
  17.       Caption         =   "Stop"
  18.       Height          =   375
  19.       Left            =   1718
  20.       TabIndex        =   6
  21.       Top             =   2940
  22.       Width           =   855
  23.    End
  24.    Begin VB.CommandButton cmdPlay 
  25.       Caption         =   "Play"
  26.       Height          =   375
  27.       Left            =   818
  28.       TabIndex        =   5
  29.       Top             =   2940
  30.       Width           =   855
  31.    End
  32.    Begin VB.Frame fraSound 
  33.       Caption         =   "Sound Type"
  34.       Height          =   1275
  35.       Left            =   60
  36.       TabIndex        =   4
  37.       Top             =   1560
  38.       Width           =   3255
  39.       Begin VB.OptionButton optSeg 
  40.          Caption         =   "DirectMusic Segment"
  41.          Height          =   255
  42.          Left            =   180
  43.          TabIndex        =   9
  44.          Top             =   900
  45.          Width           =   2655
  46.       End
  47.       Begin VB.OptionButton optMid 
  48.          Caption         =   "Midi File"
  49.          Height          =   255
  50.          Left            =   180
  51.          TabIndex        =   8
  52.          Top             =   600
  53.          Width           =   1815
  54.       End
  55.       Begin VB.OptionButton optWave 
  56.          Caption         =   "Wave File"
  57.          Height          =   255
  58.          Left            =   180
  59.          TabIndex        =   7
  60.          Top             =   300
  61.          Value           =   -1  'True
  62.          Width           =   1815
  63.       End
  64.    End
  65.    Begin VB.HScrollBar scrlPan 
  66.       Height          =   255
  67.       LargeChange     =   2
  68.       Left            =   1080
  69.       Max             =   10
  70.       Min             =   -10
  71.       TabIndex        =   1
  72.       Top             =   1200
  73.       Width           =   2235
  74.    End
  75.    Begin VB.HScrollBar scrlVol 
  76.       Height          =   255
  77.       LargeChange     =   20
  78.       Left            =   1080
  79.       Max             =   0
  80.       Min             =   -5000
  81.       SmallChange     =   500
  82.       TabIndex        =   0
  83.       Top             =   840
  84.       Width           =   2235
  85.    End
  86.    Begin VB.Image Image1 
  87.       Height          =   480
  88.       Left            =   120
  89.       Picture         =   "audtut2.frx":0442
  90.       Top             =   180
  91.       Width           =   480
  92.    End
  93.    Begin VB.Label lbl 
  94.       BackStyle       =   0  'Transparent
  95.       Caption         =   "Copyright (C) 1999-2001 Microsoft Corporation, All Rights Reserved."
  96.       Height          =   435
  97.       Index           =   2
  98.       Left            =   660
  99.       TabIndex        =   11
  100.       Top             =   300
  101.       Width           =   2655
  102.    End
  103.    Begin VB.Label lbl 
  104.       BackStyle       =   0  'Transparent
  105.       Caption         =   "Audio Tutorial 2"
  106.       Height          =   255
  107.       Index           =   0
  108.       Left            =   660
  109.       TabIndex        =   10
  110.       Top             =   60
  111.       Width           =   2655
  112.    End
  113.    Begin VB.Label Label2 
  114.       Caption         =   "Pan"
  115.       Height          =   255
  116.       Left            =   120
  117.       TabIndex        =   3
  118.       Top             =   1200
  119.       Width           =   975
  120.    End
  121.    Begin VB.Label Label1 
  122.       Caption         =   "Volume"
  123.       Height          =   255
  124.       Left            =   120
  125.       TabIndex        =   2
  126.       Top             =   840
  127.       Width           =   1095
  128.    End
  129. Attribute VB_Name = "AudTut2"
  130. Attribute VB_GlobalNameSpace = False
  131. Attribute VB_Creatable = False
  132. Attribute VB_PredeclaredId = True
  133. Attribute VB_Exposed = False
  134. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  135. '  Copyright (C) 1999-2001 Microsoft Corporation.  All Rights Reserved.
  136. '  File:       audTut2.frm
  137. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  138. '===========================================================
  139. 'DirectX Audio Tutorial 2
  140. 'This tutorial will show you how to load a wave, midi, or
  141. 'segment file in to an audio path, and then play back the
  142. 'file.  It will also show how to get an object from an audio
  143. 'path and manipulate it
  144. '===========================================================
  145. Option Explicit
  146. Private dx As DirectX8
  147. Private dml As DirectMusicLoader8
  148. Private dmp As DirectMusicPerformance8
  149. Private dmSeg As DirectMusicSegment8
  150. Private dmSegState As DirectMusicSegmentState8
  151. Private dmPath As DirectMusicAudioPath8
  152. Private mlOffset As Long
  153. Private MediaPath As String
  154. Private Sub Form_Load()
  155.     Dim dmA As DMUS_AUDIOPARAMS
  156.     MediaPath = FindMediaDir("tuta.wav")
  157.     '===========================================
  158.     '- Step 1 initialize the DirectX objects.
  159.     '===========================================
  160.     On Local Error Resume Next
  161.     Set dx = New DirectX8
  162.     Set dml = dx.DirectMusicLoaderCreate
  163.     Set dmp = dx.DirectMusicPerformanceCreate
  164.     dml.SetSearchDirectory MediaPath
  165.     '========================================================
  166.     '- Step 2
  167.     '  Now we can init our audio environment, and check for any errors
  168.     '  if a sound card is not present or DirectX is not
  169.     '  installed. The 'On Local Error Resume Next'
  170.     '  statement allows us to check error values immediately
  171.     '  after execution. The error number 0 indicates no error.
  172.     '========================================================
  173.     dmp.InitAudio Me.hWnd, DMUS_AUDIOF_ALL, dmA
  174.     If Err.Number <> 0 Then
  175.         MsgBox "Could not initialize DirectMusic." & vbCrLf & "This sample will exit.", vbOKOnly Or vbInformation, "Exiting..."
  176.         Unload Me
  177.         Exit Sub
  178.     End If
  179.     '===========================================================
  180.     '- Step 3 Now we can set up our Audio path which will play
  181.     ' the sounds.
  182.     '============================================================
  183.     Set dmPath = dmp.CreateStandardAudioPath(DMUS_APATH_DYNAMIC_3D, 64, True)
  184. End Sub
  185. Sub LoadSound(sFile As String)
  186.     '========================================================================
  187.     '- Step 4 We can now load our audio file.
  188.     ' But first we'll make sure the file exists
  189.     '=========================================================================
  190.     On Error Resume Next 'We'll check our error later in the app
  191.     If Dir$(sFile) = vbNullString Then
  192.         MsgBox "Unable to find " & sFile, vbOKOnly Or vbCritical, "Not found."
  193.         Unload Me
  194.     End If
  195.     'Cleanup if there is already a segment loaded
  196.     If Not (dmSeg Is Nothing) Then
  197.         dmSeg.Unload dmPath
  198.         Set dmSeg = Nothing
  199.     End If
  200.     Set dmSeg = dml.LoadSegment(sFile)
  201.     If Right$(sFile, 4) = ".mid" Then dmSeg.SetStandardMidiFile
  202.     '========================================
  203.     '- Step 5 Download the segment, and make sure we have no errors
  204.     '========================================
  205.     dmSeg.Download dmPath
  206.     If Err.Number <> 0 Then
  207.         MsgBox "Unable to download segment.", vbOKOnly Or vbCritical, "No download."
  208.         Unload Me
  209.     End If
  210.     scrlPan_Change
  211.     scrlVol_Change
  212. End Sub
  213. '===============================
  214. ' Step 6 - PLAYING THE SOUNDS
  215. '===============================
  216. Private Sub cmdPlay_Click()
  217.     '=========================================================
  218.     ' Make sure we've loaded our sound
  219.     '=========================================================
  220.     If dmSeg Is Nothing Then
  221.         If optWave.Value Then LoadSound MediaPath & "\tuta.wav"
  222.         If optMid.Value Then LoadSound MediaPath & "\tut.mid"
  223.         If optSeg.Value Then LoadSound MediaPath & "\sample.sgt"
  224.     End If
  225.             
  226.     '================================================
  227.     'Plays the sound
  228.     '================================================
  229.     Set dmSegState = dmp.PlaySegmentEx(dmSeg, 0, 0, Nothing, dmPath)
  230. End Sub
  231. '==================
  232. '- Step 7 Add Stop
  233. '==================
  234. Private Sub cmdStop_Click()
  235.     If dmSeg Is Nothing Then Exit Sub
  236.     dmp.StopEx dmSeg, 0, 0
  237.     mlOffset = 0
  238. End Sub
  239. '======================================================================
  240. '- Step 8 Add Handler for setting the volume
  241. '  volume is set in db and ranges from -10000 to 0
  242. '  (direct sound doesn't amplify sounds just decreases their volume)
  243. '  because db is a log scale -6000 is almost the same as
  244. '  off and changes near zero have more effect on the volume
  245. '  than those at -6000. we use a -5000 to 0
  246. '======================================================================
  247. Private Sub scrlVol_Change()
  248.     'We can just set our volume
  249.     dmPath.SetVolume scrlVol.Value, 0
  250. End Sub
  251. Private Sub scrlVol_Scroll()
  252.     scrlVol_Change
  253. End Sub
  254. '===============================================================
  255. '- Step 9 Add Handler for Pan
  256. '===============================================================
  257. Private Sub scrlPan_Change()
  258.     If dmSeg Is Nothing Then Exit Sub
  259.     'Now we need to get the corresponding Sound buffer, and make the call
  260.     Dim dsBuf As DirectSound3DBuffer8
  261.     Set dsBuf = dmPath.GetObjectinPath(DMUS_PCHANNEL_ALL, DMUS_PATH_BUFFER, 0, vbNullString, 0, "IID_IDirectSound3DBuffer")
  262.     dsBuf.SetPosition scrlPan.Value / 5, 0, 0, DS3D_IMMEDIATE
  263.     Set dsBuf = Nothing
  264. End Sub
  265. Private Sub scrlPan_Scroll()
  266.     scrlPan_Change
  267. End Sub
  268. Private Sub Form_Unload(Cancel As Integer)
  269.     'Here we will cleanup any of our code
  270.     'First we should stop any currently playing sounds
  271.     If Not (dmSeg Is Nothing) Then dmp.StopEx dmSeg, 0, 0
  272.     Set dml = Nothing
  273.     Set dmSeg = Nothing
  274.     dmp.CloseDown
  275.     Set dmPath = Nothing
  276.     Set dmp = Nothing
  277.     Set dx = Nothing
  278. End Sub
  279. 'If we click on any of the option buttons
  280. 'we should load the new a new segment
  281. Private Sub optMid_Click()
  282.     cmdStop_Click
  283.     LoadSound MediaPath & "\tut.mid"
  284. End Sub
  285. Private Sub optSeg_Click()
  286.     cmdStop_Click
  287.     LoadSound MediaPath & "\sample.sgt"
  288. End Sub
  289. Private Sub optWave_Click()
  290.     cmdStop_Click
  291.     LoadSound MediaPath & "\tuta.wav"
  292. End Sub
  293.